package com.hwl.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hwl.server.entity.Department;
import com.hwl.server.mapper.DepartmentMapper;
import com.hwl.server.pojo.RespBean;
import com.hwl.server.service.DepartmentService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author long
 * @since 2021-11-28
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Override
    public List<Department> getAllDepartments() {
        return baseMapper.getAllDepartments(-1);
    }

    @Override
    public RespBean addDep(Department department) {
        department.setEnabled(true);
        baseMapper.addDep(department);
        if (1 == department.getResult()) {
            return RespBean.success("部门添加成功！", department);
        }
        return RespBean.error("部门添加失败！");
    }

    @Override
    public RespBean deleteDep(Integer id) {
        Department department = new Department();
        department.setId(id);
        baseMapper.deleteDep(department);

        if (-2 == department.getResult()) {
            return RespBean.error("该部门下还存在子部门，删除失败！");
        }
        if (-1 == department.getResult()) {
            return RespBean.error("该部门下还有员工，删除失败！");
        }
        if (1 == department.getResult()) {
            return RespBean.success("删除部门成功！");
        }
        return RespBean.error("删除部门失败！");
    }
}
